<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
/*
	使用手册展现，还是调用系统前端框架方便。
	使用手册展现，仅第一次访问时加载分类数据和所有文章数据，后续访问仅加载具体文章内容。如何判断是第一次访问并加载分类数据
	三个op一个列表，一个搜索一个返回文章内容
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$id=intval($_RQ['id']);
	if($_RQ['type']){
		$type=intval($_RQ['type']);
	}else{
		$type=1;
	}
	$groups=pdo_getall('core_article_group',array('type'=>$type),array('id','title'),'','paixu desc,id desc');
	if($groups){
		foreach($groups as &$item){
			$item['articles']=pdo_getall('core_article',array('group_id'=>$item['id'],'status'=>1,'type'=>1),array('id','title'),'','paixu desc,id desc');
			if($item['articles']){
				foreach($item['articles'] as &$article){
					if($id && $article['id']==$id){
						$item['show']=1;
					}
					$article['url']=$_AR['siteroot'].'manage/index.php?plugin=core&action=article.show&nosession=1&id='.$article['id'];
				}
			}
		}
	}
	$return['groups']=$groups;
	if($id){
		$article1=pdo_get('core_article',array('id'=>$id,'status'=>1),array('id','title','content'));
		$return['article_show']=$article1['content'];
		
		$return['title']=$article1['title'];
		$return['url']=$_AR['siteroot'].'manage/index.php?plugin=core&action=article.show&nosession=1&id='.$article1['id'];
		$title=$article1['title'];
	}else{
		$groups['0']['show']=1;
		$return['article_show']=pdo_getcolumn('core_article',array('id'=>$groups['0']['articles']['0']['id']),'content');
		$return['title']=$groups['0']['articles']['0']['title'];
		$return['url']=$groups['0']['articles']['0']['url'];
		$title='使用手册';
	}
	$return['show']='article';
	$return['groupid']=$id ? $id : 0;
	$return['type']=$type;
	$setting=core_setting('site');
	$return['logo']=$setting['llogo']?tomedia($setting['llogo']):'';
	set_html('title',$title);
	set_html('header','common/header-cms');
	exi($return);
}
if($_RQ['op']=='sousuo'){
	//搜索，先搜索标题，再搜索内容。内容高亮如何显示？先去除html标签，然后查找关键词，以关键词为中心截取关键词上一个句号开始到100个汉字
	$keyword=trim($_RQ['keyword']);
	$articles1=pdo_getall('core_article',array('status'=>1,'title like'=>'%'.$keyword.'%'),array('id','title','content'),'id','paixu desc,id desc');
	$articles2=pdo_getall('core_article',array('status'=>1,'content like'=>'%'.$keyword.'%'),array('id','title','content'),'id','paixu desc,id desc');
	//$cs=var_export($articles1,true);
	//file_put_contents('cs.txt',$cs);
	foreach($articles1 as &$item){
		$item['title1']=str_replace($keyword,'<em>'.$keyword.'</em>',$item['title']);
		//去除html标签
		$item['content']=strip_tags($item['content']);
		//截取前一百个字符
		$item['content']=cfc('tools')->substr($item['content'],0,100);
		//高亮显示关键词
		$item['content']=str_replace($keyword,'<em>'.$keyword.'</em>',$item['content']);
		$item['url']=$_AR['siteroot'].'manage/index.php?plugin=core&action=article.show&nosession=1&id='.$item['id'];
	}
	foreach($articles2 as &$item){
		if(!$articles1[$item['id']]){
			$item['content']=strip_tags($item['content']);
			$temp=explode($keyword,$item['content']);
			$s=strrpos($temp['0'],'.');
			if($s===false){
				$s=strrpos($temp['0'],'。');
				if($s!==false){
					$s+=3;
				}
			}else{
				$s+=1;
			}
			if($s!==false){
				$item['content']=substr($item['content'],$s);
				$item['content']=cfc('tools')->substr($item['content'],0,100);
			}else{
				$item['content']=cfc('tools')->substr($item['content'],0,100);
			}
			$item['title1']=$item['title'];
			$item['content']=str_replace($keyword,'<em>'.$keyword.'</em>',$item['content']);
			$item['url']=$_AR['siteroot'].'manage/index.php?plugin=core&action=article.show&nosession=1&id='.$item['id'];
			$articles1[$item['id']]=$item;
		}
	}
	exi($articles1);
}
if($_RQ['op']=='show'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('找不到指定文章！','error');
	}
	$content=pdo_getcolumn('core_article',array('id'=>$id,'status'=>1),'content');
	//$content=base64_decode($content);
	exi($content);
}
